<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>let</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Chapter 1. Phoenix 3.2.0">
<link rel="up" href="../scope.html" title="Scope">
<link rel="prev" href="local_variables.html" title="Local Variables">
<link rel="next" href="lambda.html" title="lambda">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="local_variables.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../scope.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="lambda.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="phoenix.modules.scope.let"></a><a class="link" href="let.html" title="let">let</a>
</h4></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">phoenix</span><span class="special">/</span><span class="identifier">scope</span><span class="special">/</span><span class="identifier">let</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
          You declare local variables using the syntax:
        </p>
<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">local</span><span class="special">-</span><span class="identifier">declarations</span><span class="special">)</span>
<span class="special">[</span>
    <span class="identifier">let</span><span class="special">-</span><span class="identifier">body</span>
<span class="special">]</span>
</pre>
<p>
          <code class="computeroutput"><span class="identifier">let</span></code> allows 1..N local variable
          declarations (where N == <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LOCAL_LIMIT</span></code>).
          Each declaration follows the form:
        </p>
<pre class="programlisting"><span class="identifier">local</span><span class="special">-</span><span class="identifier">id</span> <span class="special">=</span> <span class="identifier">lambda</span><span class="special">-</span><span class="identifier">expression</span>
</pre>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
            You can set <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LOCAL_LIMIT</span></code>,
            the predefined maximum local variable declarations in a let expression.
            By default, <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LOCAL_LIMIT</span></code>
            is set to <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LIMIT</span></code>.
          </p></td></tr>
</table></div>
<p>
          Example:
        </p>
<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="number">123</span><span class="special">,</span> <span class="identifier">_b</span> <span class="special">=</span> <span class="number">456</span><span class="special">)</span>
<span class="special">[</span>
    <span class="identifier">_a</span> <span class="special">+</span> <span class="identifier">_b</span>
<span class="special">]</span>
</pre>
<p>
          <span class="bold"><strong>Reference Preservation</strong></span>
        </p>
<p>
          The type of the local variable assumes the type of the lambda- expression.
          Type deduction is reference preserving. For example:
        </p>
<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="identifier">arg1</span><span class="special">,</span> <span class="identifier">_b</span> <span class="special">=</span> <span class="number">456</span><span class="special">)</span>
</pre>
<p>
          <code class="computeroutput"><span class="identifier">_a</span></code> assumes the type of
          <code class="computeroutput"><span class="identifier">arg1</span></code>: a reference to an
          argument, while <code class="computeroutput"><span class="identifier">_b</span></code> has
          type <code class="computeroutput"><span class="keyword">int</span></code>.
        </p>
<p>
          Consider this:
        </p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>

<span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="identifier">arg1</span><span class="special">)</span>
<span class="special">[</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="special">--</span><span class="identifier">_a</span> <span class="special">&lt;&lt;</span> <span class="char">' '</span>
<span class="special">]</span>
<span class="special">(</span><span class="identifier">i</span><span class="special">);</span>

<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<p>
          the output of above is : 0 0
        </p>
<p>
          While with this:
        </p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>

<span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="identifier">val</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">))</span>
<span class="special">[</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="special">--</span><span class="identifier">_a</span> <span class="special">&lt;&lt;</span> <span class="char">' '</span>
<span class="special">]</span>
<span class="special">(</span><span class="identifier">i</span><span class="special">);</span>

<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<p>
          the output is : 0 1
        </p>
<p>
          Reference preservation is necessary because we need to have L-value access
          to outer lambda-scopes (especially the arguments). <code class="computeroutput"><span class="identifier">arg</span></code>s
          and <code class="computeroutput"><span class="identifier">ref</span></code>s are L-values.
          <code class="computeroutput"><span class="identifier">val</span></code>s are R-values.
        </p>
<p>
          <span class="bold"><strong>Visibility</strong></span> <a name="phoenix.modules.scope.let.visibility"></a>
        </p>
<p>
          The scope and lifetimes of the local variables is limited within the let-body.
          <code class="computeroutput"><span class="identifier">let</span></code> blocks can be nested.
          A local variable may hide an outer local variable. For example:
        </p>
<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">_x</span> <span class="special">=</span> <span class="identifier">_1</span><span class="special">,</span> <span class="identifier">_y</span> <span class="special">=</span> <span class="identifier">_2</span><span class="special">)</span>
<span class="special">[</span>
    <span class="comment">// _x here is an int: 1</span>

   <span class="identifier">let</span><span class="special">(</span><span class="identifier">_x</span> <span class="special">=</span> <span class="identifier">_3</span><span class="special">)</span> <span class="comment">// hides the outer _x</span>
   <span class="special">[</span>
       <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">_x</span> <span class="special">&lt;&lt;</span> <span class="identifier">_y</span> <span class="comment">// prints "Hello, World"</span>
   <span class="special">]</span>
<span class="special">](</span><span class="number">1</span><span class="special">,</span><span class="string">" World"</span><span class="special">,</span><span class="string">"Hello,"</span><span class="special">);</span>
</pre>
<p>
          The actual values of the parameters _1, _2 and _3 are supplied from the
          bracketed list at the end of the <code class="computeroutput"><span class="identifier">let</span></code>.
        </p>
<p>
          There is currently a limitation that the inner <code class="computeroutput"><span class="identifier">let</span></code>
          cannot be supplied with a constant e.g. <code class="computeroutput"><span class="identifier">let</span><span class="special">(</span><span class="identifier">_x</span> <span class="special">=</span> <span class="number">1</span><span class="special">)</span></code>.
        </p>
<p>
          The RHS (right hand side lambda-expression) of each local-declaration cannot
          refer to any LHS local-id. At this point, the local-ids are not in scope
          yet; they will only be in scope in the let-body. The code below is in error:
        </p>
<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span>
    <span class="identifier">_a</span> <span class="special">=</span> <span class="number">1</span>
  <span class="special">,</span> <span class="identifier">_b</span> <span class="special">=</span> <span class="identifier">_a</span> <span class="comment">// Error: _a is not in scope yet</span>
<span class="special">)</span>
<span class="special">[</span>
    <span class="comment">// _a and _b's scope starts here</span>
    <span class="comment">/*. body .*/</span>
<span class="special">]</span>
</pre>
<p>
          However, if an outer let scope is available, this will be searched. Since
          the scope of the RHS of a local-declaration is the outer scope enclosing
          the let, the RHS of a local-declaration can refer to a local variable of
          an outer scope:
        </p>
<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="number">1</span><span class="special">)</span>
<span class="special">[</span>
    <span class="identifier">let</span><span class="special">(</span>
        <span class="identifier">_a</span> <span class="special">=</span> <span class="identifier">_1</span>
      <span class="special">,</span> <span class="identifier">_b</span> <span class="special">=</span> <span class="identifier">_a</span> <span class="comment">// Ok. _a refers to the outer _a</span>
    <span class="special">)</span>
    <span class="special">[</span>
        <span class="comment">/*. body .*/</span>
    <span class="special">]</span>
<span class="special">](</span><span class="number">1</span><span class="special">)</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2010, 2014, 2015 Joel de Guzman, Dan Marsden, Thomas
      Heller, John Fletcher<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="local_variables.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../scope.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="lambda.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
